package github.leavesczy.reactivehttpsamples.widget.dialog;

import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;
import java.util.Objects;

import github.leavesczy.reactivehttpsamples.R;
import github.leavesczy.reactivehttpsamples.adapter.BridgeSampleSelectAdapter;
import github.leavesczy.reactivehttpsamples.core.mode.MeasureModelData;
import github.leavesczy.reactivehttpsamples.widget.dialog.base.BaseDialogFragment;
import github.leavesczy.reactivehttpsamples.widget.dialog.base.DialogLayoutCallback;
import github.leavesczy.reactivehttpsamples.widget.view.BridgeEditItem;


public class BridgeWidthCalculateDialog extends BaseDialogFragment {

    private TextView tvOk;
    private TextView tvCancel;

    public BridgeWidthCalculateDialog init(Context context,Param param,
                                           Callback callback) {
        super.init(context, new DialogLayoutCallback() {
            @Override
            public int bindTheme() {
                return R.style.CommonContentDialogStyle;
            }

            @Override
            public int bindLayout() {
                return R.layout.dialog_bridge_width_calculate;
            }

            @Override
            public void initView(final BaseDialogFragment dialog, View contentView) {
                BridgeEditItem beMotorWayNumber = contentView.findViewById(R.id.beMotorWayNumber);
                BridgeEditItem beNonMotorWayNumber = contentView.findViewById(R.id.beNonMotorWayNumber);
                BridgeEditItem beSideWalkNumber = contentView.findViewById(R.id.beSideWalkNumber);

                tvOk = contentView.findViewById(R.id.tvOk);
                tvCancel = contentView.findViewById(R.id.tvCancel);

                ClickUtils.applyPressedBgDark(tvOk);
                ClickUtils.applyPressedBgDark(tvCancel);
                tvOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String motorWayNumber = beMotorWayNumber.getEditTextStr();
                        String nonMotorWayNumber = beNonMotorWayNumber.getEditTextStr();
                        String sideWayNumber = beSideWalkNumber.getEditTextStr();
                        if (StringUtils.isEmpty(motorWayNumber)||StringUtils.isEmpty(nonMotorWayNumber)||StringUtils.isEmpty(sideWayNumber)) {
                            ToastUtils.showShort("参数不能为空");
                            return;
                        }
                        Param paramNew=new Param();
                        paramNew.setMotorWayNumber(motorWayNumber);
                        paramNew.setNonMotorWayNumber(nonMotorWayNumber);
                        paramNew.setSideWayNumber(sideWayNumber);

                        double result=2 + 3.5 * (Integer.parseInt(motorWayNumber) + Integer.parseInt(nonMotorWayNumber) + Integer.parseInt(sideWayNumber));
                        dismiss();
                        callback.ok(String.valueOf(result),paramNew);
                    }
                });

                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        callback.cancel();
                    }
                });

                if(Objects.nonNull(param)){
                    beMotorWayNumber.setEditTextStr(param.getMotorWayNumber());
                    beNonMotorWayNumber.setEditTextStr(param.getNonMotorWayNumber());
                    beSideWalkNumber.setEditTextStr(param.getSideWayNumber());
                }

            }

            @Override
            public void setWindowStyle(Window window) {
            }

            @Override
            public void onCancel(BaseDialogFragment dialog) {

            }

            @Override
            public void onDismiss(BaseDialogFragment dialog) {

            }
        });
        return this;
    }

    public interface Callback {
        void ok(String result,Param paramNew);

        void cancel();
    }

    public static class Param{
        private String motorWayNumber;
        private String nonMotorWayNumber;
        private String sideWayNumber;

        public String getMotorWayNumber() {
            return motorWayNumber;
        }

        public void setMotorWayNumber(String motorWayNumber) {
            this.motorWayNumber = motorWayNumber;
        }

        public String getNonMotorWayNumber() {
            return nonMotorWayNumber;
        }

        public void setNonMotorWayNumber(String nonMotorWayNumber) {
            this.nonMotorWayNumber = nonMotorWayNumber;
        }

        public String getSideWayNumber() {
            return sideWayNumber;
        }

        public void setSideWayNumber(String sideWayNumber) {
            this.sideWayNumber = sideWayNumber;
        }
    }
}